package com.inlycat.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Service;

import com.inlycat.entity.Account;
import com.inlycat.entity.User;
import com.inlycat.entity.UserInfo;
import com.inlycat.mapper.AccountMapper;
import com.inlycat.mapper.UserInfoMapper;
import com.inlycat.mapper.UserMapper;
import com.inlycat.service.UserService;
import com.inlycat.utils.StrUtils;

/**
 * @author jarvan4dev@163.com
 * @since 2015年8月20日下午3:44:20
 * @category
 
 **/
@Service("userService")
public class UserServiceImpl implements UserService {

	@Resource
	private UserMapper userMapper;

	@Resource
	private UserInfoMapper userInfoMapper;

	@Resource
	private AccountMapper accountMapper;

	@Override
	public Map<String, Object> register(User user) {
		Map<String, Object> userDetail = null;
		boolean result = userMapper.insert(user);
		if (result) {
			UserInfo userInfo = new UserInfo();
			userInfo.setUserId(user.getId());
			userInfo.setNickname("匿名");
			/**
			 * 添加用户信息
			 */
			result = userInfoMapper.insert(userInfo);
			if (result) {
				Account account = new Account();
				account.setUserId(user.getId());
				account.setBalance(100);
				/**
				 * 添加账户
				 */
				result = accountMapper.insert(account);
				if(result){//如果注册成功，返回用户信息
					userDetail = new HashMap<String, Object>();
					userDetail.put("account", account);
					userDetail.put("userInfo", userInfo);
				}
			}
		}
		return userDetail;
	}

	@Override
	public Map<String, Object> login(User user,HttpServletRequest request) {
		User loginUser = userMapper.selectByUsername(user.getUsername());
		if (loginUser == null) {
			return null;
		}
		String password = StrUtils.MD5Encryption(user.getPassword());
		if (!loginUser.getPassword().equals(password)) {
			return null;
		}
		/**
		 * 更新用户的访问ip和上次访问时间
		 */
		loginUser.setUsername(null);
		loginUser.setPassword(null);
		loginUser.setLastAccessIP(request.getRemoteAddr());
		loginUser.setLastAccessTime(new Date());
		userMapper.updateSelective(loginUser);
		Map<String, Object> userDetail = new HashMap<String, Object>();
		UserInfo userInfo = userInfoMapper.selectByUserId(loginUser.getId());
		Account account = accountMapper.selectByUserId(loginUser.getId());
		userDetail.put("userInfo", userInfo);
		userDetail.put("account", account);
		return userDetail;
	}

	@Override
	public boolean usernameExist(String username) {
		User user = userMapper.selectByUsername(username);
		boolean result = user == null ? false : true;
		return result;
	}

	@Override
	public boolean changePassword(String username, String password) {
		User user = new User();
		user.setUsername(username);
		user.setPassword(StrUtils.MD5Encryption(password));
		int result = userMapper.changePwd(user);
		if (result > 0) {
			return true;
		} else {
			return false;
		}
	}

	@Override
	public List<User> list(User user) {
		List<User> users = userMapper.select(user);
		return users;
	}

}
